package com.itheima.health.task;

import com.itheima.health.common.Redisconstant;
import com.itheima.health.util.AliOssUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 定时清理垃圾图片
 */
@Slf4j
@Component
public class pictask {
    private String endpoint  ;
    private String accessKeyId ;
    private String accessKeySecret ;
    private String bucketName;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private RedisTemplate redisTemplate;
   // @Scheduled(cron = "0/10 * * * * ?")
    @Scheduled(cron = "0 0 2 * * ?")
    public void delete() {
        //计算差值，然后获取垃圾图片的名称集合
        Set<String> set = redisTemplate.opsForSet().difference(Redisconstant.SETMEAL_PIC_RESOURCES, Redisconstant.SETMEAL_PIC_DB_RESOURCES);
        if(set!=null){
            for (String pathName : set) {
                //删除阿里云的垃圾图片
                String[] split = pathName.split("/");
                String picName = split[split.length - 1];
                aliOssUtil.deleteFile(picName);
            redisTemplate.opsForSet().remove(Redisconstant.SETMEAL_PIC_RESOURCES,pathName);
            log.info(pathName+"被删除了");
            }
        }
    }

}
